#include"Texture.h"

namespace Craft
{
	Texture::Texture():
		m_width(0),
		m_height(0),
		Internal_Format(GL_RGB),
		Image_Format(GL_RGB),
		Wrap_S(GL_CLAMP),
		Wrap_T(GL_CLAMP),
		Filter_Min(GL_LINEAR),
		Filter_Max(GL_LINEAR)
	{
		glGenTextures(1, &m_ID);
	}

	void Texture::generate(GLuint w, GLuint h, unsigned char* data)
	{
		m_width = w;
		m_height = h;

		glBindTexture(GL_TEXTURE_2D, m_ID);

		glTexImage2D(GL_TEXTURE_2D, 0, Internal_Format, m_width, m_height, 0, Image_Format, GL_UNSIGNED_BYTE, data);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, Wrap_S);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, Wrap_T);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
		glTexEnvf(GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS, -1.5f);
	
		gluBuild2DMipmaps(GL_TEXTURE_2D, Internal_Format, m_width, m_height, Image_Format, GL_UNSIGNED_BYTE, data);
		

		glBindTexture(GL_TEXTURE_2D, 0);
	}

	void Texture::bind(GLenum target)const
	{
		glBindTexture(GL_TEXTURE_2D, m_ID);
	}

	void Texture::set_Wrap_S(GLuint ws)
	{
		Wrap_S = ws;
	}

	void Texture::set_Wrap_T(GLuint wt)
	{
		Wrap_T = wt;
	}

	void Texture::set_Filter_Min(GLuint fm)
	{
		Filter_Min = fm;
	}

	void Texture::set_Filter_Max(GLuint fm)
	{
		Filter_Max = fm;
	}

	void Texture::set_Internal_Format(GLuint format)
	{
		Internal_Format = format;
	}

	void Texture::set_Image_Format(GLuint format)
	{
		Image_Format = format;
	}


	GLuint Texture::get_Image_Format()const
	{
		return Image_Format;
	}

}